﻿using Microsoft.Extensions.DependencyInjection;
using PmSoft;
using PmSoft.ApplicationContext;
using PmSoft.Exceptions;

namespace Modules.System.AppNotice
{
    [UseDI(ServiceLifetime.Scoped)]
    public class AppNoticeInfoService
    {
        private readonly AppNoticeInfoRepostitory appNoticeInfoRepostitory;
        private readonly IApplicationContext applicationContext;

        public AppNoticeInfoService(AppNoticeInfoRepostitory appNoticeInfoRepostitory, IApplicationContext applicationContext)
        {
            this.appNoticeInfoRepostitory = appNoticeInfoRepostitory;
            this.applicationContext = applicationContext;
        }

        public IPagedList<AppNoticeInfo> GetAppNoticeInfos(AppNoticeInfoQuery query, int pageIndex, int pageSize)
        {
            return appNoticeInfoRepostitory.GetAppNoticeInfos(query, pageIndex, pageSize);
        }

        public AppNoticeInfo? GetAppNoticeInfo(int? id)
        {
            return appNoticeInfoRepostitory.Get(id);
        }

        /// <summary>
        /// 新增通知公告
        /// </summary>
        /// <param name="appNoticeInfo"></param>
        public void CreateAppNoticeInfo(AppNoticeInfo appNoticeInfo)
        {
            appNoticeInfoRepostitory.Insert(appNoticeInfo);
        }

        /// <summary>
        /// 编辑通知公告
        /// </summary>
        /// <param name="appNoticeInfo"></param>
        public void UpdateAppNoticeInfo(AppNoticeInfo appNoticeInfo)
        {
            appNoticeInfoRepostitory.Update(appNoticeInfo);
        }

        /// <summary>
        /// 置顶通知公告，只允许置顶一条数据
        /// </summary>
        /// <param name="id"></param>
        /// <exception cref="BusinessException"></exception>
        public void UpdateAppNoticeInfoTop(AppNoticeInfo appNoticeInfo)
        {
            try
            {
                applicationContext.Transaction.Begin();//开始事务
                UpdateAppNoticeInfo(appNoticeInfo);

                var noticeInfos = appNoticeInfoRepostitory.GetAppNoticeInfos();//查询所有有效数据
                var filterList = noticeInfos.Where(x => x.Id != appNoticeInfo.Id);
                foreach (var item in filterList)
                {
                    item.IsTop = false;
                    item.UpdateTime = DateTime.Now;
                    UpdateAppNoticeInfo(item);
                }
                applicationContext.Transaction.Complete();//完成事务
            }
            catch (Exception ex)
            {
                applicationContext.Transaction.Abort();//回滚事务
                throw new BusinessException($"置顶失败");
            }
        }

        /// <summary>
        /// 删除通知公告
        /// </summary>
        /// <param name="ids"></param>
        public void DeletAppNoticeInfos(List<int> ids)
        {
            foreach (int id in ids)
            {
                //DeletAppNoticeInfos(id);
                var data = GetAppNoticeInfo(id);
                if (data != null)
                {
                    data.IsDeleted = true;
                    data.UpdateTime = DateTime.Now;
                    UpdateAppNoticeInfo(data);
                }
            }
        }

        /// <summary>
        /// 删除通知公告
        /// </summary>
        /// <param name="roleId"></param>
        public void DeletAppNoticeInfos(int id)
        {
            appNoticeInfoRepostitory.DeleteByEntityId(id);
        }
    }
}